package com.daihou.BCJ.resource;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.daihou.config.TheAuth;
import com.daihou.model.ceres.ResourceUrlEnum;
import com.daihou.utils.DHConfigFile;
import com.daihou.utils.DHHttpUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.Assert;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @author liujintao
 * @date 2021/2/3 15:39
 */
public class TaskSkill {
    private static final Logger logger = LoggerFactory.getLogger(TaskSkill.class);
    private String url;
    Map<String, String> headers = new HashMap<>();
    private int id;

    @BeforeTest(description = "测试准备工作,获取Authorization对象")
    public void beforeTest() throws IOException {
        url = DHConfigFile.getUrl("DaiHou_URL","resource", ResourceUrlEnum.TALKSKILLINSERT.toString());
        //设置请求头中的Authorization
        headers.put("Authorization", TheAuth.getAuth());
        headers.put("X-UserName", "admin");
    }

    @Test(description = "话术管理插入数据")
    public void taskSkillInsertTest() {
        String body = "{\"id\":\"\",\"talkSkillName\":\"tt\",\"companyId\":\"IFLYTEK\",\"talkSkillId\":\"16493\",\"talkSkill\":[{\"talkSkillField\":\"客户手机号码\",\"afterLoanField\":\"overDueAmt\",\"remark\":\"\"},{\"talkSkillField\":\"产品名称\",\"afterLoanField\":\"productType\",\"remark\":\"\"},{\"talkSkillField\":\"客服热线\",\"afterLoanField\":\"custName\",\"remark\":\"\"},{\"talkSkillField\":\"逾期总金额\",\"afterLoanField\":\"mobileNo\",\"remark\":\"\"},{\"talkSkillField\":\"客户姓名\",\"afterLoanField\":\"gender\",\"remark\":\"\"},{\"talkSkillField\":\"性别\",\"afterLoanField\":\"overDueAmt\",\"remark\":\"\"}]}";
        String result = Objects.requireNonNull(DHHttpUtil.post_with_String(url, body, headers)).toJSONString();
        logger.info("插入结果:{}", result);
        Assert.assertTrue(result.contains("\"message\":\"新增话术已存在\",\"status\":\"-1\""));
    }

    @Test(description = "话术管理查询", dependsOnMethods = "taskSkillInsertTest")
    public void taskSkillQueryTest() {
        url = DHConfigFile.getUrl("DaiHou_URL","resource", ResourceUrlEnum.TALKSKILLQUERY.toString());
        JSONObject body = new JSONObject();
        body.put("pageNum", 1);
        body.put("pageSize", 20);
        String result = Objects.requireNonNull(DHHttpUtil.post_with_String(url, body.toJSONString(), headers)).toJSONString();
        logger.info("查询结果:{}", result);
        Assert.assertTrue(result.contains("\"message\":\"操作成功\",\"status\":\"0\""));
        id = JSON.parseObject(result).getJSONObject("data").getJSONArray("list").getJSONObject(0).getIntValue("id");
    }

    @Test(description = "话术管理更新数据", dependsOnMethods = "taskSkillQueryTest")
    public void taskSkilllUpdateTest() {
        url = DHConfigFile.getUrl("DaiHou_URL","resource", ResourceUrlEnum.TALKSKILLUPDATE.toString());
        String body = "{\"id\":" + id + ",\"talkSkillName\":\"test01\",\"companyId\":\"IFLYTEK\",\"talkSkillId\":\"16493\",\"talkSkill\":[{\"talkSkillField\":\"客户手机号码\",\"afterLoanField\":\"productType\",\"remark\":\"\"},{\"talkSkillField\":\"产品名称\",\"afterLoanField\":\"productType\",\"remark\":\"\"},{\"talkSkillField\":\"客服热线\",\"afterLoanField\":\"productType\",\"remark\":\"\"},{\"talkSkillField\":\"逾期总金额\",\"afterLoanField\":\"productType\",\"remark\":\"\"},{\"talkSkillField\":\"客户姓名\",\"afterLoanField\":\"productType\",\"remark\":\"\"},{\"talkSkillField\":\"性别\",\"afterLoanField\":\"productType\",\"remark\":\"\"}]}";
        String result = Objects.requireNonNull(DHHttpUtil.post_with_String(url, body, headers)).toJSONString();
        logger.info("更新结果:{}", result);
        Assert.assertTrue(result.contains("\"message\":\"操作成功\",\"status\":\"0\""));
    }

    @Test(description = "话术管理删除数据", dependsOnMethods = "taskSkilllUpdateTest")
    public void taskSkillDeleteTest() {
        url = DHConfigFile.getUrl("DaiHou_URL","resource", ResourceUrlEnum.TALKSKILLDEL.toString());
        JSONObject body = new JSONObject();
        body.put("id", String.valueOf(id));
        String result = Objects.requireNonNull(DHHttpUtil.post_with_String(url, body.toJSONString(), headers)).toJSONString();
        logger.info("删除结果:{}", result);
        Assert.assertTrue(result.contains("\"message\":\"该话术已被使用，无法删除！\",\"status\":\"-1\""));
    }
}